<?php
defined("APP_VERSION") OR die("No Access");
class IndexAction extends CoreAction {
	public $member = null;
	
	public function initAction() {
		//对所有的GET POST 数据进行格式化
		$_GET = $this->htmlToScript($_GET);
		$_POST = $this->htmlToScript($_POST);
		
		$this->data['links'] = array();
		$this->data['keywords'] = isset($_GET['keywords']) ? $_GET['keywords'] : '';
		$this->data['page'] = isset($_GET['page']) ? $_GET['page'] : 1;
		$this->data['area'] = isset($_GET['area']) ? $_GET['area'] : '';
 	}
	
	/**
	 * 点评网首页. 
	 */
	public function index() {
		$this->data['links'][] = 'css/base-old.v8.css';
		$this->data['links'][] = 'css/common-old.v6.css';
		$this->display();
	}
	
	/**
	 * LF点评页面. 
	 */
	public function female() {
		$this->data['links'][] = 'css/c.mylist.v7.css';
		$this->data['links'][] = 'css/c.shop.v142.css';
		$this->data['links'][] = 'css/g.base.v218.css';
		$this->data['links'][] = 'css/g.reviewform.v47.css';
		
		$women_id = isset($_GET['women_id']) ? intval($_GET['women_id']) : '';
		$women_model = new WomenModel();
		$this->data['women'] = $women_model->getWomenById($women_id);
		$this->display();
	}
	
	/**
	 * 点评页面. 
	 */
	public function review() {
		$this->data['links'][] = 'css/g.base.v218.css';
		$this->data['links'][] = 'css/c.remark.v59.css';
			
		$this->display();
	}
	
	/**
	 * 特别推荐.  
	 */
	public function recommend() {
		$this->data['links'][] = 'css/g.base.v218.css';
		$this->data['links'][] = 'css/c.search.v83.css';
		$this->display();
	}
	
	/**
	 * 同城交友 
	 */
	public function friend() {
		$this->data['links'][] = 'css/g.base.v218.css';
		$this->data['links'][] = 'css/c.search.v83.css';
		
		$women_model = new WomenModel();
		$this->data['womens'] = $women_model->getList();
		$this->data['pages'] = $women_model->page($women_model->count, $this->data['page'], 18);
		$this->display();
	}
	
	/**
	 * 检索页 
	 */
	public function search() {
		$this->data['links'][] = 'css/g.base.v218.css';
		$this->data['links'][] = 'css/c.search.v83.css';	

		$condation = array();
		if ($this->data['keywords'] != '') {
			$condation[] = "`nickname` LIKE '%".$this->data['keywords']."%'";
		}
		
		if ($this->data['area'] != '') {
			$condation[] = "`location` = '".$this->data['area']."'";
		}
		
		$condation = "1=1 AND ".implode(" AND ", $condation)." ORDER BY `createtime` DESC";
		$women_model = new WomenModel();
		$this->data['limit'] = 18;
		$this->data['womens'] = $women_model->getList($condation, $this->data['page'], $this->data['limit']);
		$this->data['total'] = $women_model->count;
		$this->data['pages'] = $women_model->page($this->data['total'], $this->data['page'], $this->data['limit']);
		$this->display();
	}
	
	/**
	 * 女友宿舍
	 */
	public function girl() {
		$this->data['links'][] = 'css/g.base.v218.css';
		$this->data['links'][] = 'css/c.search.v83.css';
		$this->data['links'][] = 'css/girl.css';
		
		$this->data['validate_code'] = md5(time());
		$this->setCookie('VALIDATECODE', $this->data['validate_code']);
				
		//处理表单提交的数据.
		if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
			//如果非本站提交数据,不允许录入.
			if($_COOKIE['VALIDATECODE'] != $_POST['validate_code']) {
				$this->message('back', '非法数据提交.');
			}
			
			$women_model = new WomenModel();
			if ($women_model->addData() == false) {
				$this->message('back', '你输入的数据有误,请重新输入.');
			} else {
				$this->data['women'] = $women_model->getData();
				$this->display('success.php');
				exit;
			}
		}
		
		$this->display();
	}
	
	/**
	 * 设置用户cookie信息. 
	 */
	protected function setCookie($name, $value) {
		setcookie($name, $value, time()+900, '/');
	}
	
	/**
	 * 建立公用js文件. 
	 */
	public function createCommJs() {
		$comm_js_dir = APP_ROOT.DIRECTORY_SEPARATOR.'js'.DIRECTORY_SEPARATOR;
		$comm_js_file = $comm_js_dir.'comm.js';
		
		file_put_contents($comm_js_file, $this->fetch());
	}
}